﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace org.xisoft.game.utils.game.algorithms
{
    /**
     * 极大极限值
     */
    public abstract class NegaMaxAlgorithm<Broad> : AiAlgorithm<Broad>
                 where Broad : ChessBoardBase
    {
        public override MoveAction Search(Broad broad, int depth)
        {
            List<MoveAction> points = GetAllCanMoves(broad);
            MoveAction bestMove = null;

            foreach (MoveAction move in points)
            {
                if (depth > 0)
                {
                    broad.DoNextMove(move);
                    MoveAction action = Search(broad, depth - 1);
                    broad.Undo();

                    if (action != null)
                        move.Score -= action.Score;
                    move.Next = action;
                }

                if (bestMove == null || bestMove.Score < move.Score)
                    bestMove = move;


            }

            return bestMove;
        }

    }
}
